package com.zsl.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.zsl.bean.OrderBean;
import com.zsl.dao.OrderDao;
import com.zsl.order.db.OrderDB;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;


public class OrderDaoImpl implements OrderDao{
	private OrderDB dbHelper=null;
	private SQLiteDatabase database=null;
	private Cursor cursor=null;
	
	
	public OrderDaoImpl(Context context) {
		super();
		// TODO Auto-generated constructor stub
		this.dbHelper=new OrderDB(context);
		this.database=dbHelper.getWritableDatabase();
	}
	
//	@Override
//	public boolean save(Talkmessage talkmessage) {
//		// TODO Auto-generated method stub
//		String sqlString="insert into talkmessage(me, username, questionid , name, message, messagetime, isread , messageidentify, messagetype , issend )values(?,?,?,?,?,?,?,?,?,?)";
//		try {
//			database.execSQL(sqlString, new String[]{talkmessage.getMe(),talkmessage.getUsername(),talkmessage.getQuestionid(),talkmessage.getName(),talkmessage.getMessage(),talkmessage.getMessagetime(),talkmessage.getIsread(),talkmessage.getMessageidentify(),talkmessage.getMessagetype(),talkmessage.getIssend()});
//			return true;
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		
//		return false;
//	}
//
//	@Override
//	public List<Talkmessage> findNoReadMSG(Talkmessage talkmessage) {
//		// TODO Auto-generated method stub
//		String sqlString="select * from talkmessage where me=? and username=? and isread=0";
//		List<Talkmessage> talkmessages=new ArrayList<Talkmessage>();
//		try {
//			cursor=database.rawQuery(sqlString, new String[]{talkmessage.getMe(),talkmessage.getUsername()});
//			while (cursor.moveToNext()) {
//				Talkmessage talkmessage2=new Talkmessage();
//				talkmessage2.setMe(talkmessage.getMe());
//				talkmessage2.setUsername(talkmessage.getUsername());
//				talkmessage2.setQuestionid(cursor.getString(cursor.getColumnIndex("questionid")));
//				talkmessage2.setName(talkmessage.getName());
//				talkmessage2.setMessage(cursor.getString(cursor.getColumnIndex("message")));
//				talkmessage2.setMessagetime(cursor.getString(cursor.getColumnIndex("messagetime")));
//				talkmessage2.setIsread(cursor.getString(cursor.getColumnIndex("isread")));
//				talkmessage2.setMessageidentify(cursor.getString(cursor.getColumnIndex("messageidentify")));
//				talkmessage2.setMessagetype(cursor.getString(cursor.getColumnIndex("messagetype")));
//				talkmessages.add(talkmessage2);
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		return talkmessages;
//	}
//
//	@Override
//	public List<Talkmessage> findMsgTopOne(Talkmessage talkmessage) {
//		// TODO Auto-generated method stub
//		String sqlString="select * from talkmessage where questionid=? limit 100";
//		
//		List<Talkmessage> talkmessages=new ArrayList<Talkmessage>();
//		try {
//			cursor=database.rawQuery(sqlString, new String[]{talkmessage.getQuestionid()});
//			while (cursor.moveToNext()) {
//				Talkmessage talkmessage2=new Talkmessage();
//				talkmessage2.setMe(cursor.getString(cursor.getColumnIndex("me")));
//				talkmessage2.setUsername(cursor.getString(cursor.getColumnIndex("username")));
//				talkmessage2.setQuestionid(cursor.getString(cursor.getColumnIndex("questionid")));
//				talkmessage2.setName(cursor.getString(cursor.getColumnIndex("name")));
//				talkmessage2.setMessage(cursor.getString(cursor.getColumnIndex("message")));
//				talkmessage2.setMessagetime(cursor.getString(cursor.getColumnIndex("messagetime")));
//				talkmessage2.setIsread(cursor.getString(cursor.getColumnIndex("isread")));
//				talkmessage2.setMessageidentify(cursor.getString(cursor.getColumnIndex("messageidentify")));
//				talkmessage2.setMessagetype(cursor.getString(cursor.getColumnIndex("messagetype")));
//				talkmessages.add(talkmessage2);
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		return talkmessages;
//	}
	
	//create table order(id int autocomplete,ordername varchar(6000),money int,time varchar(50))

	@Override
	public boolean save(OrderBean orderBean) {
		// TODO Auto-generated method stub
		String sql="insert into orderList(ordername,money,ordertime) values(?,?,?)";
		try {
			database.execSQL(sql, new String[]{orderBean.getOrderName(),orderBean.getMoney(),orderBean.getTime()});
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public List<OrderBean> findAllByToday(String Time) {
		// TODO Auto-generated method stub
		String sql="select * from orderList where ordertime=?";
		List<OrderBean> orderBeans=new ArrayList<OrderBean>();
		try {
			cursor=database.rawQuery(sql,new String[]{Time});
			while (cursor.moveToNext()) {
				OrderBean orderBean=new OrderBean();
				orderBean.setOrderName(cursor.getString(cursor.getColumnIndex("ordername")));
				orderBean.setMoney(cursor.getString(cursor.getColumnIndex("money")));
				orderBean.setTime(cursor.getString(cursor.getColumnIndex("ordertime")));
				orderBeans.add(orderBean);
			}
			return orderBeans;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			cursor.close();
		}
		
		return null;
	}

	
	
}
